/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.gps;

import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.widget.Toast;

import id.duapuluhdua.dakidaki.Preferences;
import id.duapuluhdua.dakidaki.R;
import id.duapuluhdua.dakidaki.UserAlert;
import id.duapuluhdua.dakidaki.activities.ActivityMain;

/**
 *
 */
public class GpsActivityReader extends GpsReader
{
  /** Fields */
  private String mLocatingPositionMsg;

  private boolean bShowMsgOnce;
  private boolean bShowSettingsOnce;
  private boolean bVibrated;

  /**
   * The default constructor.
   *
   * @param parent the application main activity.
   * @param prefs the application settings.
   */
  public GpsActivityReader(ActivityMain parent, Preferences prefs)
  {
    super(parent, prefs);

    mLocatingPositionMsg = mContext.getString(R.string.notify_msg_gps_is_locating);

    bShowMsgOnce = true;
    bShowSettingsOnce = true;
  }

  /**
   * Starts the GPS service.
   */
  public void start()
  {
    mPreferences.load();
    super.start(mPreferences.getLocationUpdateInterval());

    if (isEnabled()) {
      if (bShowMsgOnce) {
        Toast.makeText(mContext, mLocatingPositionMsg, Toast.LENGTH_LONG).show();
        bShowMsgOnce = false;
      }
    }
    else {
      showLocationSettings();
    }
  }

  private DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
      if (which == DialogInterface.BUTTON_POSITIVE) {
        // Show location settings
        mContext.startActivity(new Intent("android.settings.LOCATION_SOURCE_SETTINGS"));
      }
    }
  };

  protected void showLocationSettings()
  {
    if (!isEnabled()) {
      if (bShowSettingsOnce) {
        bShowSettingsOnce = false;
        UserAlert.question(mContext, R.string.alert_question_enable_loc_service, listener);
      }
    }
  }

  @Override
  protected void onGpsLocationChanged(Location location, boolean accuracyAcceptable)
  {
    if (accuracyAcceptable) {
      if (!bVibrated) {
        vibrate();
        bVibrated = true;
      }
    }
    else {
      bVibrated = false;
    }
    // Post location to global position object
    ActivityMain.loader.getGpsLocationStatus().postLocation(location);
  }

  /**
   * Restarts the GPS service.
   */
  public void restart()
  {
    stop();
    start();
  }
}
